/**@FILE:OTSaTableDataOperation.java
 * @AUTHOR:qingsheng.zheng
 * @DATE:2015-3-27 下午1:52:40*/
package com.fz.tool.aliyun.ots;

import java.util.ArrayList;
import java.util.List;

import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.ServiceException;
import com.aliyun.openservices.ots.OTSClient;
import com.aliyun.openservices.ots.OTSException;
import com.aliyun.openservices.ots.model.Condition;
import com.aliyun.openservices.ots.model.Direction;
import com.aliyun.openservices.ots.model.GetRangeRequest;
import com.aliyun.openservices.ots.model.GetRangeResult;
import com.aliyun.openservices.ots.model.GetRowRequest;
import com.aliyun.openservices.ots.model.GetRowResult;
import com.aliyun.openservices.ots.model.PutRowRequest;
import com.aliyun.openservices.ots.model.PutRowResult;
import com.aliyun.openservices.ots.model.RangeRowQueryCriteria;
import com.aliyun.openservices.ots.model.Row;
import com.aliyun.openservices.ots.model.RowExistenceExpectation;
import com.aliyun.openservices.ots.model.RowPrimaryKey;
import com.aliyun.openservices.ots.model.RowPutChange;
import com.aliyun.openservices.ots.model.SingleRowQueryCriteria;

/*******************************************
 * @COMPANY:alibaba
 * @CLASS:OTSaTableDataOperation
 * @DESCRIPTION:
 * @AUTHOR:qingsheng.zheng
 * @VERSION:v1.0
 * @DATE:2015-3-27 下午1:52:40
 *******************************************/
public class OTSTableDataOperator {
	final String endPoint = "";
	final String accessId = "";
	final String accessKey = "";
	final String instanceName = "";
	private OTSClient client;

	public OTSTableDataOperator(String endPoint, String accessId,
			String accesskey, String instanceName) {
		client = new OTSClient(endPoint, accessId, accesskey, instanceName);
	}

	public static OTSTableDataOperator getInstance(String endPoint,
			String accessId, String accesskey, String instanceName) {
		return new OTSTableDataOperator(endPoint, accessId, accesskey,
				instanceName);
	}

	public void putRow(String tableName, List<OTSRow> rows) throws Exception {
		RowPutChange rowChange = genRowChange(tableName, rows);
		PutRowRequest request = new PutRowRequest();
		request.setRowChange(rowChange);
		PutRowResult result = client.putRow(request);
		int consumedWriteCU = result.getConsumedCapacity().getCapacityUnit()
				.getWriteCapacityUnit();
	}
	

	public void putRow(String tableName, OTSRow row) throws Exception {
		List<OTSRow> list=new ArrayList<OTSRow>();
		list.add(row);
		this.putRow(tableName, list);
	}
	
	
	

	public static RowPutChange genRowChange(String tableName, List<OTSRow> rows) {
		RowPutChange rowChange = null;
		RowPrimaryKey primaryKey = null;
		for (int i = 0; i < rows.size(); i++) {
			OTSRow row = rows.get(i);
			rowChange = new RowPutChange(tableName);
			primaryKey = new RowPrimaryKey();
			List<PrimaryValue> primaryValues = row.getPrimaryValues();
			List<AttrValue> attrValues = row.getAttrValues();
			for (int j = 0; j < primaryValues.size(); j++) {
				primaryKey.addPrimaryKeyColumn(primaryValues.get(j)
						.getColname(), primaryValues.get(j).getValue());
			}
			rowChange.setPrimaryKey(primaryKey);
			for (int h = 0; h < attrValues.size(); h++) {
				rowChange.addAttributeColumn(attrValues.get(h).getColname(),
						attrValues.get(h).getValue());
			}
		}
		rowChange.setCondition(new Condition(
				RowExistenceExpectation.EXPECT_NOT_EXIST));
		return rowChange;

	}

	private static Row getRow(OTSClient client, String tableName,
			List<PrimaryValue> queryPrimarys, String[] cols)
			throws ServiceException, ClientException {

		SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(tableName);
		RowPrimaryKey primaryKeys = new RowPrimaryKey();
		for (int i = 0; i < queryPrimarys.size(); i++) {
			PrimaryValue primaryValue = queryPrimarys.get(i);
			primaryKeys.addPrimaryKeyColumn(primaryValue.getColname(),
					primaryValue.getValue());
		}
		criteria.setPrimaryKey(primaryKeys);
		criteria.addColumnsToGet(cols);
		GetRowRequest request = new GetRowRequest();
		request.setRowQueryCriteria(criteria);
		GetRowResult result = client.getRow(request);
		Row row = result.getRow();

		int consumedReadCU = result.getConsumedCapacity().getCapacityUnit()
				.getReadCapacityUnit();
		return row;
	}

	public  List<Row> getRange( String tableName,
			List<PrimaryValue> start, List<PrimaryValue> end,Long limit,Boolean forward)
			throws OTSException, ClientException {
		// 演示一下如何按主键范围查找，这里查找uid从1-4（左开右闭）的数据
		RangeRowQueryCriteria criteria = new RangeRowQueryCriteria(tableName);
		RowPrimaryKey inclusiveStartKey = new RowPrimaryKey();
		for (int i = 0; i < start.size(); i++) {
			PrimaryValue primaryValue = start.get(i);
			inclusiveStartKey.addPrimaryKeyColumn(primaryValue.getColname(),
					primaryValue.getValue());
		}
		RowPrimaryKey exclusiveEndKey = new RowPrimaryKey();
		for (int i = 0; i < end.size(); i++) {
			PrimaryValue primaryValue = end.get(i);
			exclusiveEndKey.addPrimaryKeyColumn(primaryValue.getColname(),
					primaryValue.getValue());
		}
        criteria.setDirection(forward?Direction.FORWARD:Direction.BACKWARD);
		criteria.setInclusiveStartPrimaryKey(inclusiveStartKey);
		criteria.setExclusiveEndPrimaryKey(exclusiveEndKey);
		
		GetRangeRequest request = new GetRangeRequest();
		request.setRangeRowQueryCriteria(criteria);
		GetRangeResult result = client.getRange(request);
		List<Row> rows = result.getRows();
		int consumedReadCU = result.getConsumedCapacity().getCapacityUnit()
				.getReadCapacityUnit();
		return rows;
	}
}
